package com.qijian.common.utils.ip;

import com.alibaba.fastjson.JSONObject;
import com.qijian.common.config.QiJianConfig;
import com.qijian.common.constant.Constants;
import com.qijian.common.utils.StringUtils;
import com.qijian.common.utils.http.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 获取地址类
 *
 * @author ruoyi
 */
public class AddressUtils {
    private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);

    // IP地址查询
    public static final String PCONLINE_IP_URL = "https://whois.pconline.com.cn/ipJson.jsp";
    public static final String TB_IP_URL = "https://ip.taobao.com/outGetIpInfo";
    public static final String API_IP_URL = "http://ip-api.com/json";
//    public static final String USERAGENTINFO_IP_URL = "https://ip.useragentinfo.com/json";

    // 未知地址
    public static final String UNKNOWN = "XX XX";

    public static String getRealAddressByIP(String ip) {
        String address = UNKNOWN;
        // 内网不查询
        if (IpUtils.internalIp(ip)) {
            return "内网IP";
        }
        if (QiJianConfig.isAddressEnabled()) {
            try {
                address = pconline(ip);
            } catch (Exception e) {
                // log.error("获取地理位置异常 {}", ip);
            }
        }
        return address;
    }


    private static String pconline(String ip) {
        String address;
        String rspStr = HttpUtils.sendGet(PCONLINE_IP_URL, "ip=" + ip + "&json=true", Constants.GBK);
        JSONObject obj = JSONObject.parseObject(rspStr);
        String region = obj.getString("pro");
        String city = obj.getString("city");
        String isp = obj.getString("addr");
//        address = StringUtils.trim(String.format("%s", region, city, isp));
        address = StringUtils.trim(isp);
        if (StringUtils.isEmpty(address)) {
            address = taobao(ip);
        }
        return address;
    }

    private static String taobao(String ip) {
        String address;
        String rspStr = HttpUtils.sendGet(TB_IP_URL, "ip=" + ip + "&accessKey=alibaba-inc", Constants.UTF8);
        JSONObject obj = JSONObject.parseObject(rspStr);
        if (obj.getInteger("code") == 0) {
            obj = obj.getJSONObject("data");
            String country = obj.getString("country");
            String region = obj.getString("region");
            String city = obj.getString("city");
            String isp = obj.getString("isp");
            address = String.format("%s %s %s %s", country, region, city, isp);
            if (StringUtils.isBlank(address)) {
                address = ipApi(ip);
            }
        } else {
            address = ipApi(ip);
        }
        return address;
    }

    private static String ipApi(String ip) {
        String address;
        String rspStr = HttpUtils.sendGet(API_IP_URL, "//" + ip + "?lang=zh-CN", Constants.UTF8);
        JSONObject obj = JSONObject.parseObject(rspStr);
        String country = obj.getString("country");
        String region = obj.getString("regionName");
        String city = obj.getString("city");
        address = String.format("%s %s %s", country, city, region);
        return address;
    }

//    private static String useragentinfo(String ip) {
//        String address;
//        String rspStr = HttpUtils.sendGet(USERAGENTINFO_IP_URL, "ip=" + ip, Constants.UTF8);
//        JSONObject obj = JSONObject.parseObject(rspStr);
//        if (obj.getInteger("code") == 200) {
//            String country = obj.getString("country");
//            String region = obj.getString("province");
//            String city = obj.getString("city");
//            String isp = obj.getString("isp");
//            address = String.format("%s %s %s %s", country, region, city, isp);
//            if (StringUtils.isBlank(address)) {
//                address = pconline(ip);
//            }
//        } else {
//            address = pconline(ip);
//        }
//        return address;
//    }
}
